package com.pz.gulimall.ware.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.Query;

import com.pz.gulimall.ware.dao.PurchaseDetailDao;
import com.pz.gulimall.ware.entity.PurchaseDetailEntity;
import com.pz.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String status = (String) params.get("status");
        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and( w -> {
                w.eq("id", key).or()
                  .eq("purchaseId", key).or()
                  .eq("sku_id", key);
            });
        }

        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status", status);
        }

        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据 purchaseId 获取所有的 PurchaseDetailEntity
     * @param id
     * @return
     */
    @Override
    public List<PurchaseDetailEntity> listByPurchaseId(Long id) {
        if( id != null){
            return this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id",id));
        }
        return new ArrayList<>();
    }

}